/*
 * admin_firmware.c
 *
 *  Created on: 2023年2月23日
 *      Author: Lw
 */
#include "admin_firmware.h"
#include "dri_hal_uart.h"
#include "ff.h"
#include "xqspipsu.h"		/* QSPIPSU device driver */
#include "dri_hal_qspi.h"
#include "dri_hal_fatfs.h"
#include "update_firmware.h"
#include "sys.h"
#include "tele_data_transfer.h"
/* 定义传输的文件大小为32M  */
#define FILE_MAX_SIZE 			33554432
u8 FileBuff[FILE_MAX_SIZE];
#define QSPIPSU_DEVICE_ID		XPAR_XQSPIPSU_0_DEVICE_ID

#define BOOT_MODE_USER_REG  0x00FF5E0200

extern S_tEquipmentStatus g_tEquipmentStatus;      /* 设备的状态信息结构体 */
/*
 * The instances to support the device drivers are global such that they
 * are initialized to zero each time the program runs. They could be local
 * but should at least be static so they are zeroed.
 */
static XQspiPsu QspiPsuInstance;

int Emmc2QflashFirmware(void);
int Qflash2EmmcFirmware(void);


void adminFirmwareInit(void)
{
	u32 BootType;
	BootType = ReadBootType();
	xil_printf("BootTyep = %d \r\n", BootType);

	if(BootType == 2){
		g_tEquipmentStatus.ucStartMode = CORE_START_QSPI;
	}
	else {
		g_tEquipmentStatus.ucStartMode = CORE_START_EMMC;
	 }

	s_AdFw.cmd = 0;
	s_AdFw.key = 0;
}

/*向协FPGA发送切emmc固件指令*/
void SendChangeEmmcMsg(void)
{
    u8 acChangeCommand[3] = {0xF, 0xAA, 0xB9};
    XUartPs_Send(&UartPs_0, acChangeCommand, sizeof(acChangeCommand));
}
/*向协FPGA发送切flash固件指令*/
void SendChangeFlashMsg(void)
{
    u8 acChangeCommand[3] = {0xf, 0x55, 0x64};
    XUartPs_Send(&UartPs_0, acChangeCommand, sizeof(acChangeCommand));
}

void adminFirmwareProcess(void)
{
	if(s_AdFw.cmd == 0x11 && s_AdFw.key == 0xAA){
		s_AdFw.cmd = 0;
		s_AdFw.key = 0;
		 Qflash2EmmcFirmware();
	}
	if(s_AdFw.cmd == 0x22 && s_AdFw.key == 0xAA){
		s_AdFw.cmd = 0;
		s_AdFw.key = 0;
		Emmc2QflashFirmware();
	}
}

/*emmc固件覆盖到Qflash中*/
int Emmc2QflashFirmware(void)
{
	int Status;
	xil_printf("start emmc to flash update system!\r\n");
	Status = qspi_init(&QspiPsuInstance);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	xil_printf("init flash successful\r\n");
	Status = emmcReadData(EMMC_UPDATE_FILE_NAME, FileBuff, FILE_MAX_SIZE);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	xil_printf("read emmc successful\r\n");
	Status = update_qspi(&QspiPsuInstance, FILE_MAX_SIZE, FileBuff);
	xil_printf("flash Successful update \r\n");
	return Status;
}

/*Qflash固件覆盖到emmc中*/
int Qflash2EmmcFirmware(void)
{
	int Status;
	xil_printf("start flash to emmc update system!\r\n");
	Status = qspi_init(&QspiPsuInstance);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	xil_printf("init flash successful\r\n");
	Status = read_qspi_fw(&QspiPsuInstance, FILE_MAX_SIZE, FileBuff);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	xil_printf("Read flash successful\r\n");
	f_unlink(EMMC_UPDATE_FILE_NAME);
	Status = emmcWriteData(EMMC_UPDATE_FILE_NAME, FileBuff, FILE_MAX_SIZE, 0);
	if (Status != FR_OK) {
		return XST_FAILURE;
	}
	xil_printf("emmc Successful update \r\n");
	return 0;
}

/*获取启动方式*/
u32 ReadBootType(void)
{
	return Xil_In32(BOOT_MODE_USER_REG);
}
